package cn.xinzhi.servlet;

import cn.xinzhi.entity.UserInfo;
import cn.xinzhi.service.impl.UpdateUserInfoServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "UpdateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        String userid = request.getParameter("userid");
        String username = request.getParameter("username");
        String userage = request.getParameter("userage");
        System.out.println(username);

        UserInfo ui = new UserInfo();
        if (userage != null){
            ui.setUserage(Integer.parseInt(userage));
        }
        if (userid != null){
            ui.setUserid(Integer.parseInt(userid));
        }
        ui.setUsername(username);

        UpdateUserInfoServiceImpl uii = new UpdateUserInfoServiceImpl();
        String medUser = uii.mendUser(ui,(List<UserInfo>) request.getSession().getAttribute("nowPage"));
        out.println(JSON.toJSON(medUser));

        out.flush();
        out.close();


    }
}
